package com.ym.controller.soso;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.annotation.Log;
import com.ym.core.config.ConfigCenterProperties;
import com.ym.core.config.QiNiuYunConfig;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.soso.domain.entity.AppHealthPlateEntity;
import com.ym.soso.domain.param.AppHealthPlateQueryParam;
import com.ym.soso.domain.vo.AppHealthPlateListMobileVO;
import com.ym.soso.service.IAppHealthPlateSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.nio.charset.Charset;
import java.util.function.Function;

/**
 * 健康科普Controller
 *
 * @author ym
 * @date 2020-08-24
 */
@Api(tags = "soso-健康科普")
@RestController
@Validated
@RequestMapping(value = "/soso/plate", headers = Constants.Api.VERSION)
public class AppHealthPlateController extends BaseController {

	@Autowired
	private IAppHealthPlateSV appHealthPlateSV;

	@Autowired
	private ConfigCenterProperties configCenterProperties;


	@ApiOperation("查询健康科普列表")
	@Log(dataBase = false, printBody = false)
	@GetMapping("/list")
	public APIResponse<PageInfo<AppHealthPlateListMobileVO>> list(@Valid AppHealthPlateQueryParam queryParam) {
		PageDomain pageDomain = PageDomain.getPageDomain();
		PageInfo<AppHealthPlateListMobileVO> page = appHealthPlateSV.pageMobile(pageDomain.getPageNum(),pageDomain.getPageSize(),queryParam);
		return successData(page);
	}

	@ApiOperation("获取健康科普详细信息")
	@Log(dataBase = false, printBody = false)
	@GetMapping(value = "/info")
	public APIResponse<AppHealthPlateEntity> info(@RequestParam Long id) {
		AppHealthPlateEntity data = appHealthPlateSV.selectById(id);
		return successData(data);
	}

	@Log(printBody = false)
	@ApiOperation("健康科普详情")
	@GetMapping(value = "/health/detail")
	public APIResponse<AppHealthPlateEntity> healthDetail(@RequestParam Long id, @RequestParam int width) {
		AppHealthPlateEntity data = appHealthPlateSV.selectByIdCustom(id, width);
		return successData(data);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("健康科普富文本显示")
	@GetMapping(value = "/health/show/{id}", produces = MediaType.TEXT_HTML_VALUE)
	public void healthShow(@PathVariable Long id, @RequestParam int width, HttpServletResponse rsp) throws Exception {
		rsp.setContentType(MediaType.TEXT_HTML_VALUE);
		rsp.setCharacterEncoding(Charset.defaultCharset().name());
		// 显示网页信息
		QiNiuYunConfig qiNiuYunConfig = configCenterProperties.getQiNiuYunConfig();
		AppHealthPlateEntity appHealthPlate = appHealthPlateSV.selectByIdCustom(id, width);
		// 处理空字符串
		Function<Object, Object> handleString = new Function<Object, Object>() {
			@Override
			public Object apply(Object s) {
				if (s == null) {
					return "";
				}
				return s;
			}
		};


		if (appHealthPlate != null) {
			try (ServletOutputStream out = rsp.getOutputStream()) {
				IoUtil.writeUtf8(
						out,
						true,
						"<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\">",
						"<title>健康搜一搜</title>",
						"</head>",
						"<body style=\"padding: 5px 15px;\" >",
						"<div style=\"color: #33333D;font-weight: bold; font-size: 16px; text-align: 'flex-start';\">",
						handleString.apply(appHealthPlate.getTitle()),
						"</div>",
						"<div style=\"display: flex; flex-direction: row; justify-content: space-between; margin-top: 8px;\">",
						"<div style=\"color: #999999; font-size: 12px;\">" + DateUtil.format(appHealthPlate.getPublishTime(), "yyyy-MM-dd HH:mm") + "</div>",
						"  <div style=\"color: #999999; font-size: 12px;\">" + handleString.apply(appHealthPlate.getReadNum()) + "人阅读</div>",
						"</div>",
						"<div style=\"margin-top: 15px; display: flex; flex-direction: row; margin-bottom: 15px;\">",
						"<img style=\"width: 33px; height: 33px;\" src=\"" + qiNiuYunConfig.getShowImageUrl() + appHealthPlate.getHeadUrl() + "\">",
						"<div style=\"margin-left: 12px;\">",
						" <div style=\"display: flex; flex-direction: row;\">",
						"<div style=\"font-size: 14px; color: #333333; font-weight: bold; margin-right: 8px;\">" + handleString.apply(appHealthPlate.getAuthor()) + "</div>",
						"<div style=\"font-size: 13px; color: #666666; margin-right: 13px;\">" + handleString.apply(appHealthPlate.getDeptName()) + "</div>",
						" <div style=\"font-size: 13px; color: #666666; margin-right: 4px;\">" + handleString.apply(appHealthPlate.getJobName()) + "</div>",
						" </div>",
						"  <div style=\"font-size: 12px; color: #666666;margin-top: 5px;\">" + handleString.apply(appHealthPlate.getHosName()) + "</div>",
						"</div>",
						"</div>",
						" <div>",
						appHealthPlate.getContent(),
						"</div>",
						"<hr style=\"height:1px;background-color: #ededed;border: none;margin: 30px -15px 15px -15px\">",
						"<div style='color:#999999;font-size: 13px; marigin:15px 0 15px 0;text-align:left'>内容来源：有来医生</div>",
						"</body></html>"
				);
			}
		}
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("健康科普阅读或者转发量+1")
	@GetMapping(value = "/health/inc")
	public APIResponse healthInc(@RequestParam Long id, @RequestParam @Pattern(regexp = "01|02") String type) {
		appHealthPlateSV.readInc(id, type);
		return success();
	}

}
